$(function () {
    let ws;
    if("WebSocket" in window) {
        // 获取招聘者id
        let clientID = $("#eid").val();
        // 创建WebSocket对象,需要注意请求路径
        ws = new WebSocket("ws://127.0.0.1:8081/hire/web/" + clientID);
        // 接收到服务端消息时触发
        ws.onmessage = function (evt) {
            let received_msg = evt.data;
            layer.msg(received_msg,{
                offset: '50px'
            });
        };
    }
});
layui.use(['jquery','table', 'layer','form','laydate'], function () {
    let $ = layui.$;
    let table = layui.table;
    let layer = layui.layer;
    let form = layui.form;
    let laydate = layui.laydate;
    laydate.render({
        elem: '#cal',
        type: 'datetime'
    });
    table.render({
        elem: '#invitation',
        title: '邀约面试',
        url: '/hire/employer/interview/data',
        response: {
            statusName: 'code',
            statusCode: 200,
            msgName: 'msg',
            count: 'count',
            dataName: 'data'
        },
        height: "625px",
        page: true,
        limit: 10,
        limits: [2, 5, 10, 20],
        cols: [[
            {type: 'checkbox', fixed: 'left'},
            {title: '序号', templet: '#xuhao', width: 100, align: 'center', sort: true},
            {
                field: 'id', hide: true, title: 'id', templet: function (res) {
                    return res.interview.id;
                }
            },
            {field: 'recruitmentName', title: '职位名称',width:130, align: 'center'},
            {
                field: 'name', title: '求职者', align: 'center', width:85, templet: function (res) {
                    return res.resume.name;
                }
            },
            {
                field: 'contact_phone', title: '联系电话', align: 'center',width:130, templet: function (res) {
                    return res.resume.phone;
                }
            },
            {
                field: 'contact_email', title: '联系邮箱', align: 'center', templet: function (res) {
                    return res.resume.email;
                }
            },
            {
                field: 'invitation_time', title: '邀约时间', width: 200,  align: 'center', templet: function (res) {
                    return res.interview.interviewTime;
                }
            },
            {
                field: 'invitation_address', title: '邀约地点', align: 'center', templet: function (res) {
                    return res.interview.interviewAddress;
                }
            },
            {field: 'relationName', toolbar: '#barDemo', title: '操作', align: 'center'}
        ]],
        text: {none: '您没有邀约信息'}
    });
    table.on('tool(invitation)', function (obj) {
        // console.log(obj);
        let id = obj.data.interview.id;
        // console.log("id="+id);
        if (obj.event == 'update') {
            form.val('interview-form', {
                // "resumeId": obj.data.interview.resumeId,
                // "recruitmentId": obj.data.interview.recruitmentId,
                "interviewId":obj.data.interview.id,
                "recruitment": obj.data.recruitmentName,
                "interviewTime": obj.data.interview.interviewTime,
                "interviewAddress":obj.data.interview.interviewAddress,
            });
            let index = layer.open({
                type: 1,
                title: '邀约面试',
                skin: 'layui-layer-molv',
                content: $('#interview-layer'),
                cancel: function () {
                    // 右上角关闭事件的逻辑
                    layer.close(index);
                    $("#interview-layer").css("display", "none");
                }
            });
            form.on('submit(interview-submit)', function () {
                // let id = obj.data.delivery.id;
                let param = $("#interview-form").serialize();
                // console.log(param)
                param = param + "&deliveryId=" + id;
                $.post('/hire/employer/interview/update', param, function (res) {
                    console.log(param);
                    layer.close(index);
                    $("#interview-layer").css("display", "none");
                    if (res.code == '200') {
                        layer.msg('修改成功', {icon: 6});
                        // table.reload('invitation',{where:that.where});
                        // table.reload('resume1', {
                        //     ulr: '/hire/employer/resume/data?status=有意向'
                        // });
                        table.reload('invitation');
                        // obj.del();
                    } else {
                        layer.msg("很抱歉,请您重试", {icon: 5});
                    }
                });
                return false;
            })
            // window.location.href = "/hire/employer/edit?id=" + id;
        } else if (obj.event == 'withdraw') {
            layer.confirm('请确认是否取取消面试邀约', {
                btn: ['确认', '取消'] //按钮
            }, function () {
                // console.log("正在执行取消操作...");
                let param = {id:id};
                // console.log(param);
                $.post('/hire/employer/interview/delete',param,function (res) {
                    if(res.code=='200'){
                        layer.msg("取消成功",{icon:1});
                        obj.del();
                    }
                });

            });
        }
    });

});
